package com.bnlr.lrparking.widget.popcity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.SizeUtils;

/**
 * 下拉选择城市
 */

public class PopupCheckBox extends android.support.v7.widget.AppCompatCheckBox implements PopupWindow.OnDismissListener{
    private PopupWindow popupWindow;
    private Context context;
    private int screenWidth;
    private int screenHeight;
    public PopupCheckBox(Context context,int screenHeight) {
        super(context);
        this.context = context;
    }

    public PopupCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initCheck(context);
    }

    public void initCheck(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        screenHeight = wm.getDefaultDisplay().getHeight();
    }
    public void setPopupWindow(final View view) {
        setPopupView(view,screenHeight);
    }
    public void setDropCity(View view) {
        setPopupView(view,screenHeight- SizeUtils.dp2px(100));
    }
    public void setPopupView(final View view, final int mscreenHeight) {
        android.util.Log.i("TAG", "method setPopupView start !");
        this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }

                } else {
                    if(popupWindow == null) {
                        LinearLayout layout = new LinearLayout(context);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mscreenHeight);
                        view.setLayoutParams(params);
                        layout.addView(view);
                        layout.setBackgroundColor(Color.argb(60, 0, 0, 0));
                        popupWindow = new PopupWindow(layout,screenWidth,screenHeight);
                        popupWindow.setFocusable(true);
                        popupWindow.setBackgroundDrawable(new BitmapDrawable());
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.setOnDismissListener(PopupCheckBox.this);
                        layout.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                popupWindow.dismiss();
                            }
                        });
                    }
                    if (Build.VERSION.SDK_INT < 24) {
                        popupWindow.showAsDropDown(PopupCheckBox.this);
                    } else {
                        int[] a = new int[2];
                        PopupCheckBox.this.getLocationInWindow(a);
                        popupWindow.showAtLocation(PopupCheckBox.this, Gravity.NO_GRAVITY, 0, buttonView.getHeight()+a[1]);
                        popupWindow.update();
                    }
                }
            }
        });
android.util.Log.i("TAG", "method setPopupView over !");
    }

    public PopupCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }
    public void hidePopup(){
        if(popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    @Override
    public void onDismiss() {
        this.setChecked(false);
    }
}
